/**
 * This file is part of decent-cpp.
 *
 * decent-cpp is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * decent-cpp is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with decent-cpp.  If not, see <http://www.gnu.org/licenses/>.
 */

#define DC_UTIL_EXPORT_ERRORS
#include "util.h"
#include <chrono>
#include <exception>
#include <regex>

using std::chrono::duration_cast;
using std::chrono::seconds;
using std::chrono::system_clock;
using std::regex;
using std::regex_match;

namespace dc {
std::string const& version_api() {
  static std::string version(DECENT_API_VERSION);
  return version;
}
std::string const& version_impl() {
  static std::string version(DECENT_CPP_VERSION);
  return version;
}

u64_t timestamp() {
  return duration_cast<seconds>(system_clock::now().time_since_epoch())
      .count();
}

const regex name_regex("^[a-zA-Z0-9_\\-]+$");

bool name_valid(std::string const& name) {
  return regex_match(name, name_regex);
}

const std::string error_strings[] = {
#define DC_ERROR(x) std::string(#x)
    DC_ERRORS
#undef DC_ERROR
};

std::string const& error_to_string(Error error) {
  size_t idx = (size_t)error;
  if (idx >= (size_t)Error::SIZE) {
    throw std::invalid_argument("Gib real error thank");
  }
  return error_strings[idx];
}
}  // namespace dc